package fr.umlv.ig.main;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.io.PrintWriter;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;

public class Paint {
	
	static final JTextArea station = new JTextArea();
	static final JTextArea nextStation = new JTextArea();
	static final JTextArea direction = new JTextArea();
	static final JFrame frame = new JFrame("metro");
	
	public static void main(String[] args) throws FileNotFoundException {

		final PrintWriter writer = new PrintWriter("edge.xml");
		
		frame.setSize(906,623);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		GridLayout grid = new GridLayout(1, 5);
		JPanel panel = new JPanel(grid);
		
		panel.add(new JLabel("station:",JLabel.CENTER));
		panel.add(station);
		panel.add(new JLabel("nextStation:",JLabel.CENTER));
		panel.add(nextStation);
		panel.add(new JLabel("direction:",JLabel.CENTER));
		panel.add(direction);

		frame.getContentPane().add(panel,BorderLayout.NORTH);
		frame.getContentPane().add(ex3(writer));
		JButton button = new JButton("Close file");
		
		frame.getContentPane().add(button,BorderLayout.SOUTH);

		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				writer.close();
				frame.dispose();
			}
		});
		
		frame.setResizable(false);
		frame.setVisible(true);
		
	}
	

	
	public static JScrollPane ex3(final PrintWriter writer) throws FileNotFoundException {
		final JLabel corner=new JLabel();
		corner.setHorizontalAlignment(SwingConstants.CENTER);
		JLabel image=new JLabel(new ImageIcon(Paint.class.getResource("paris.jpg")));
		image.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseExited(MouseEvent e) {
				corner.setText("hors zone");
			}
		});
		//a ajouter a la main;

			
		//a ajouter a la main	
		image.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				corner.setText("("+e.getX()+","+e.getY()+")");
				frame.setTitle(station.getText());
				System.out.println("<station name=\""+station.getText()+"\" x=\""+e.getX()+"\" y=\""+e.getY()+"\" />");
				
				writer.write("<edge station=\""+station.getText()+"\" next=\""+nextStation.getText()+"\" />\n");
			}
		});
		JScrollPane scroll=new JScrollPane(image);
		scroll.setRowHeaderView(new JLabel(new ImageIcon(Paint.class.getResource("side.gif"))));
		scroll.setColumnHeaderView(new JLabel(new ImageIcon(Paint.class.getResource("top.gif"))));
		scroll.setCorner(ScrollPaneConstants.UPPER_LEFT_CORNER,corner);
		return scroll;

	}	
	
	

	
}
